package com.wfs.myapp.entity.vo;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Data
@ToString
public class PageResponse<T> {
    public static final Integer PAGE_SIZE = 10;
    private List<T> records = new ArrayList<>();
    private Pagination pagination;
    private Long total;

    public PageResponse(List<T> data, Long total, Integer page, Integer size) {
        this.records = data;
        this.total = total;
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setSize(size);
        pagination.setPages(total == 0L ? 1 : (int)Math.ceil((double)total / (double)size));
        pagination.setTotal(total);
        pagination.setHasNext(pagination.getPages() > page);
        pagination.setHasPrevious(1 < page);
        this.pagination = pagination;
    }

    @Data
    @ToString
    public static class Pagination {
        private Long total;
        private Integer page;
        private Integer size;
        private Boolean hasNext;
        private Boolean hasPrevious;
        private Integer pages;

        public Pagination() {
        }

    }
}
